package com.lry.sso.service.redis.service.impl;

import com.alibaba.fastjson.JSON;
import com.lry.sso.service.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * redis接口实现
 *
 * @author 李瑞益
 * @date 2019/12/9
 */
@Service
public class RedisServiceImpl implements RedisService {

//    @Autowired
//    private RedisActuator redisActuator;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key,value);
//        redisActuator.set(key,value);
    }

    @Override
    public void set(String key, String value, int expireTime) {
        redisTemplate.opsForValue().set(key,value,expireTime, TimeUnit.SECONDS);
//        redisActuator.setex(key,value,expireTime);
    }

    @Override
    public <T> void setObject(String key, T object, int expireTime) {
        redisTemplate.opsForValue().set(key, JSON.toJSONString(object),expireTime,TimeUnit.SECONDS);
//        redisActuator.setObject(key, object,expireTime);
    }

    @Override
    public String get(String key) {
        return JSON.toJSONString(redisTemplate.opsForValue().get(key));
//        return redisActuator.get(key);
    }

    @Override
    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
//        return redisActuator.exists(key);
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
//        redisActuator.delete(key);
    }

    @Override
    public void expire(String key, int seconds) {
        redisTemplate.expire(key,seconds,TimeUnit.SECONDS);
//        redisActuator.expire(key,seconds);
    }

    @Override
    public void setrange(String key, String value) {
        redisTemplate.opsForValue().set(key,value,0);
//        redisActuator.setrange(key,value);
    }

    @Override
    public Long pttl(String key) {
        return redisTemplate.getExpire(key);
//        return redisActuator.pttl(key);
    }
}
